package com.main.beatit;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class SoundManager {
	private  SoundPool mSoundPool;
	private  HashMap<Integer, Integer> mSoundPoolMap; 
	private  AudioManager  mAudioManager;
	private  Context mContext;
	
	private  HashMap<Integer, MediaPlayer> mMediaPlayerMap; 
	
	public SoundManager()
	{
		
	}
		
	public void initSounds(Context theContext) { 
		 mContext = theContext;
	     mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0); 
	     mSoundPoolMap = new HashMap<Integer, Integer>(); 
	     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);

	     mMediaPlayerMap = new HashMap<Integer, MediaPlayer>(); 
	} 

	public void addSound(int Index, int SoundID)
	{
		mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
	}
	
	public void addMediaSound(int Index, int SoundID)
	{
		mMediaPlayerMap.put(Index, MediaPlayer.create(mContext, SoundID));
	}
	
	public int playSound(int index) { 
		
	     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
	     return mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
	}
	
	public void playMediaSound(int index) { 
		
	     mMediaPlayerMap.get(index).start(); 
	}
	
	public int playLoopedSound(int index) { 
		
	     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
	     return mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
	}
	
	public void stopSound(int streamID) {
		
		mSoundPool.stop(streamID);
	}
	
	public void stopMediaSound(int index) {
		
		mMediaPlayerMap.get(index).stop();
	}
	
	public void setRate(int streamID, float rate)
	{
		mSoundPool.setRate(streamID, rate);
	}
	
	public void setVolume(int streamID, float volume)
	{
		mSoundPool.setVolume(streamID, volume, volume);
	}
	
	public void setMediaVolume(int index, float volume)
	{
		mMediaPlayerMap.get(index).setVolume(volume, volume);
	}
	
}